package structure.composite;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lzp
 * @Date:2023/2/24
 * @description: 可包含多个节点的元素标签节点
 */
public class ElementNode implements Node {

	private String name;

	private List<Node> list = new ArrayList<>();

	public ElementNode(String name) {
		this.name = name;
	}

	@Override
	public Node add(Node node) {
		list.add(node);
		return this;
	}

	@Override
	public List<Node> children() {
		return list;
	}

	@Override
	public String toXML() {
		// 拼接左半边标签
		StringBuffer sb = new StringBuffer("<").append(name).append(">\n");
		list.forEach(node -> sb.append(node.toXML()));
		// 拼接根右半边标签
		sb.append("</").append(name).append(">\n");
		return sb.toString();
	}
}
